package com.ruoyi.project.applet.controller;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.applet.domain.map.TencentResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@Api(tags = "地图接口")
@RequestMapping("/map")
public class MapController {


    @Value("${map.location}")
    private String mapLocation;


    @Autowired
    RestTemplate restTemplate;


    @ApiOperation(value = "逆地址解析")
    @GetMapping("/location")
    public AjaxResult location(@RequestParam double longitude,@RequestParam double latitude) {
        String location = latitude+","+longitude;
        String url = String.format(mapLocation, location);
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<Object> httpEntity = new HttpEntity<>(httpHeaders);
        ResponseEntity<TencentResponse> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, TencentResponse.class);
        TencentResponse responseBody = response.getBody();
        Integer status = responseBody.getStatus();
        if (status == 0 ){
            return AjaxResult.success(responseBody.getResult());
        }
        return AjaxResult.error(responseBody.getMessage());
    }


}
